<?php

use yii\bootstrap\BootstrapAsset;
use common\models\shop\ShopGoods;
use backend\modules\business\models\BusinessPig;
use yii\db\Query;
use common\models\shop\ShopOrderGoods;
use common\models\shop\ShopOrder;
use common\models\shop\ShopRaisingOrder;
use common\models\business\BusinessCost;
use yii\helpers\ArrayHelper;
use common\models\business\BusinessBackpig;


BootstrapAsset::register($this);


$sql = 'select o.ordersn,og.price,o.status from '.ShopOrderGoods::tableName().' as og left join '.ShopGoods::tableName().' as g on og.goodsid=g.id left join '.ShopOrder::tableName().' as o on og.orderid=o.id where g.pigid=:id and o.status>=:status';
$list = Yii::$app->db->createCommand($sql)
    ->bindValues([':status'=>ShopOrder::STATUS_PAY,':id'=>$model->id])
    ->queryAll();

/***********应收***************/
$sum = 0;
$nopay = [];
foreach ($list as $key => $row){
    if($row['status'] >= ShopOrder::STATUS_PAY){
        $nopay[] = $row;
        $sum +=$row['price'];
    }
}
$yingshusum = $sum + $model->price;




/***********已收***************/
//猪肉
$sum = 0;
$getmoney = [];
foreach ($list as $key => $row){
    if($row['status'] >= ShopOrder::STATUS_PAY){
        $getmoney[] = $row;
    }
}
$getmoney = $getmoney?$getmoney:[];
//认养
$sql = 'select price,ordersn from '.ShopRaisingOrder::tableName().' where status>=:status and pigid=:pigid';
$rasing = Yii::$app->db->createCommand($sql)
    ->bindValues([':status'=>ShopRaisingOrder::STATUS_PAY,':pigid'=>$model->id])
    ->queryOne();
if(!empty($list)){
    foreach ($list as $key => $row){
        $sum += $row['price'];
    }
}

$yishusum = $sum+$rasing['price'];



/***********应付***************/
$sum = 0;
$sql = 'select price,type,status from '.BusinessCost::tableName().' where pigid=:pigid';
$cost = Yii::$app->db->createCommand($sql)->bindValues([':pigid'=>$model->id])->queryAll();
if(!empty($cost)){
    foreach ($cost as $key =>$row){
        $sum +=$row['price'];
    }
}

//回购仔猪
$sql = 'select backprice,status from '.BusinessBackpig::tableName().' where pigid=:pigid';
$back = Yii::$app->db->createCommand($sql)->bindValues([':pigid'=>$model->id])->queryOne();

if($back['status'] == BusinessBackpig::STATUS_AGREE){
    $backnopay = $backpay = $back['backprice'];
}else{
    $backnopay = $back['backprice'];
    $backpay = 0;
}

$yingfusum = $sum+$backnopay ;




/***********已付***************/
$sum = 0;
$hcost = [];
if(!empty($cost)){
    foreach ($cost as $key => $row)
    {
        if($row['status'] == BusinessCost::STATUS_CONFIRM){
            $hcost[] = $row;
            $sum += $row['price'];
        }
    }
}

$yifusum = $sum+$backpay

?>



<div class="row">
    <div class="col-md-3">
        <h4>应收</h4>
        <table class="table table-hover">
            <tr>
                <th>猪肉</th>
                <th>价格</th>
            </tr>
            <?php $total=0; ?>
            <?php foreach ($nopay as $key => $row){ ?>
            <tr>
                <td><?=$row['ordersn']?></td>
                <td><?=$row['price']?></td>
            </tr>
                <?php $total+=$row['price']; ?>
            <?php } ?>
            <tr>
                <td>合计</td>
                <td><?=$total?></td>
            </tr>
        </table>
        <table class="table table-hover">
            <tr>
                <th>认养</th>
                <th>价格</th>
            </tr>
            <tr>
                <td><?=$model->no?></td>
                <td><?=$model->price?></td>
            </tr>
        </table>
    </div>
    <div class="col-md-3">
        <h4>已收</h4>
        <table class="table table-hover">
            <tr>
                <th>猪肉</th>
                <th>价格</th>
            </tr>

            <?php $total=0; ?>
            <?php foreach ($getmoney as $key => $row){ ?>
                <tr>
                    <td><?=$row['ordersn']?></td>
                    <td><?=$row['price']?></td>
                </tr>
                <?php $total+=$row['price']; ?>
            <?php } ?>
            <tr>
                <td>合计</td>
                <td><?=$total?></td>
            </tr>
        </table>
        <table class="table table-hover">
            <tr>
                <th>认养</th>
                <th>价格</th>
            </tr>
            <tr>
                <td><?=$rasing['ordersn']?></td>
                <td><?=$rasing['price']?></td>
            </tr>
        </table>
    </div>
    <div class="col-md-3">
        <h4>应付</h4>
        <table class="table table-hover">
            <tr>
                <th>仔猪费</th>
            </tr>
            <tr>
                <td><?=$model->cost ?></td>
            </tr>
        </table>
        <table class="table table-hover">
            <tr>
                <th>类型</th>
                <th>成本</th>
            </tr>
            <?php $total=0; ?>
            <?php foreach ($cost as $key => $row){ ?>
                <tr>
                    <td><?=$row['type']?></td>
                    <td><?=$row['price']?></td>
                </tr>
                <?php $total+=$row['price']; ?>
            <?php } ?>
            <tr>
                <td>合计</td>
                <td><?=$total?></td>
            </tr>
        </table>
        <table class="table table-hover">
            <tr>
                <th>回购仔猪价格</th>
                <th>价格</th>
            </tr>
            <tr>
                <td><?= $model->no ?></td>
                <td><?= $backnopay?$backnopay:'待回购...'?></td>
            </tr>
        </table>
    </div>
    <div class="col-md-3">
        <h4>已付</h4>
        <table class="table table-hover">
            <tr>
                <th>仔猪费</th>
            </tr>
            <tr>
                <td><?=$model->cost ?></td>
            </tr>
        </table>
        <table class="table table-hover">
            <tr>
                <th>类型</th>
                <th>成本</th>
            </tr>
            <?php $total=0; ?>
            <?php foreach ($hcost as $key => $row){ ?>
                <tr>
                    <td><?=$row['type']?></td>
                    <td><?=$row['price']?></td>
                </tr>
                <?php $total+=$row['price']; ?>
            <?php } ?>
            <tr>
                <td>合计</td>
                <td><?=$total?></td>
            </tr>
        </table>

        <table class="table table-hover">
            <tr>
                <th>回购仔猪价格</th>
                <th>价格</th>
            </tr>
            <tr>
                <td><?= $model->no ?></td>
                <td><?= $backpay?$backpay:'待回购...' ?></td>
            </tr>
        </table>
    </div>


    <div class="col-md-12">
        <h4>合计</h4>
        <div class="col-md-6">
            <table class="table table-hover">
                <tr>
                    <th>应收</th>
                    <th>应付</th>
                    <th>应的利润</th>
                </tr>
                <tr>
                    <td><?= $yingshusum ?></td>
                    <td><?= $yingfusum ?></td>
                    <td><?= $yingshusum-$yifusum ?></td>
                </tr>
            </table>
        </div>
        <div class="col-md-6">
            <table class="table table-hover">
                <tr>
                    <th>已收</th>
                    <th>已付</th>
                    <th>既得利润</th>
                </tr>
                <tr>
                    <td><?= $yishusum ?></td>
                    <td><?= $yifusum ?></td>
                    <td><?= $yishusum-$yifusum ?></td>
                </tr>
            </table>
        </div>
    </div>
</div>


